<?php
namespace app\admin\controller;
use app\common\controller\Base;
use think\Request;
use think\Db;
use think\Lang;
use think\Controller;
use clt\Menu;
use think\Cookie;
use app\admin\model\Messages;
use app\admin\model\Users;
class Common extends Base
{
    protected $mod,$role,$system,$nav,$menudata,$cache_model,$categorys,$module,$moduleid,$adminRules,$HrefId;
    public $pageSize=10;
    public function _initialize()
    {
        parent::_initialize();
        //判断管理员是否登录
        if (!session('aid')) {
            if($this->request->isPost()){
                echo json_encode(['code'=>10000,'msg'=>lang('Please login first.'),'url'=>U('/')]);
                exit;
            }else{
                $this->redirect('user/login/index');
            }
        }
        
        $this->User_id = session('aid');
        $this->user = db('users')->where('admin_id',$this->User_id)->find();
        if(empty($this->user)){
            $this->redirect('login/index');
        }
        
        $auth = new \clt\Auth;
        if(!$auth->verifyAuth()){
            return $this->error(lang("sorry, you don't have permission!"));
        }   
        $limit = $this->request->request('limit');
        if(intval($limit) > 0){
            Cookie('limit',$limit);
            $this->pageSize=$limit;
        }
        $Menu = new Menu;
        $this->system = F('System');
        $this->categorys = F('Category');
        $this->module = F('Module');
        $this->mod = F('Mod');
        $this->role = F('Role');
        $this->cache_model=array('Module','Role','Category','Posid','Field','System');
        if(empty($this->system)){
            foreach($this->cache_model as $r){
                savecache($r);
            }
        }
        
        /*左邊菜單欄目*/
        $navList = array();
        $this->assign('menus', $Menu->getMenus($navList));
        $this->assign('navList', $navList);
        $this->assign('lang', $this->lang);
    }
    //空操作
    public function _empty(){
        return $this->error(lang('Empty operation, return to the last access page...'));
    }
    public function export($fileName, $headArr, $data,$ext='xlsx'){
        header("Content-type: text/html; charset=utf-8");
        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能import导入
        import("Excel.PHPExcel",PLUGIN_PATH);
        import("Excel.PHPExcel.Writer.Excel2007",PLUGIN_PATH);
        import("Excel.PHPExcel.Writer.Excel5",PLUGIN_PATH);
        import("Excel.PHPExcel.IOFactory",PLUGIN_PATH);
        import("Excel.PHPExcel.CSV",PLUGIN_PATH);
        $date = date("Y-m-d", time());
        $fileName = iconv("utf-8", "gb2312", $fileName);
        if (empty($fileName)) {
            $fileName = date("Y-m-dHis", time());
        }else{
            $fileName .= "-{$date}";
        }
        //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();
        //设置表头
        $key = ord("A");
        $headIndex = 1;
        $objActSheet = $objPHPExcel->getActiveSheet();
        if(!empty($baseHead) && is_array($baseHead)){
            foreach ($baseHead as $k=>$v) {
                $colum = chr($key).$headIndex;
                if($k>1){
                    $cellcolum = chr($key+$k-1).$headIndex;
                    $objActSheet->mergeCells($colum.':'.$cellcolum);
                }
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum, $v);
                $k = $k==0?1:$k;
                $key += $k;
            }
            $headIndex = 2;
        }
        $key = ord("A");
        foreach ($headArr as $v) {
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . $headIndex, $v);
            if($colum=='A' || $colum=='I' || $colum=='J'){
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($colum)->setWidth(15);
            }else{
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($colum)->setWidth(25);
            }
            
            $key += 1;
        }
        $column = $headIndex+1;
        foreach ($data as $key => $rows) { //行写入
            $span = ord("A");
            foreach ($rows as $keyName => $value) {// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j . $column, $value);
                $span++;
            }
            $column++;
        }
        /*导出文件*/
        switch ($ext) {
            case 'xls':
                # code...
                $exrObj = 'Excel5';
                $fileName .=".xls";
                break;
            case 'csv':
                # code...
                $exrObj = 'CSV';
                $fileName .=".csv";
                break;
            default:
                # code...
                $exrObj = 'Excel2007';
                $fileName .=".xlsx";
                break;
        }
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->getActiveSheet()->setTitle("Cloud Fast"); 
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, $exrObj);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output'); //文件通过浏览器下载
        exit;
    }
    public function notice($network,$sup_id=0){
        $vars = is_array($network) ? $network : $network->toArray();
        switch ($vars['status']) {
            case 1:  //推送消息给客户服务
                # code...
                $vars['noticeType'] = 1;
                $vars['surname'] = Users::getEmail($vars['user_id']);
                $vars['title'] = lang("Application notification of customer network line opening:");
                $temp = $this->fetch('message/temp',$vars);
                if($sup_id==1){
                    Messages::Notice((int)$sup_id,lang('Notifications for application of network lines'),$temp,1,2);
                }else{
                    Messages::Notice((int)$sup_id,lang('Notifications for application of network lines'),$temp,1,1);
                }
                break;
            case 2:  //推送消息给客户和他的上级
                if($vars['pre_status'] == 8){ //网络调整
                    $vars['noticeType'] = 22;
                    $vars['title'] = lang("Hello, your network adjustment application is passed.");
                    $temp = $this->fetch('message/temp',$vars);
                    Messages::Notice($vars['user_id'],lang('Notifications of application for broadband adjustment of network lines'),$temp,4,0);
                    /*推送给上级和*/
                    $sup_id = Users::getSupId($vars['user_id']);
                    $vars['noticeType'] = 2;
                    $vars['surname'] = Users::getEmail($vars['user_id']);
                    $vars['title'] = lang("Hello,%s's application for network adjustment has passed.",[$vars['surname']]);
                    $temp = $this->fetch('message/temp',$vars);
                    Messages::Notice($sup_id,lang('Notifications of application for broadband adjustment of network lines'),$temp,4,1);
                    if(isset($vars['engineeId']) && $vars['engineeId'] > 0){
                        $vars['title'] = lang("Hello, your configured network line configuration has been passed by the administrator.");
                        $temp = $this->fetch('message/temp',$vars);
                        Messages::Notice($vars['engineeId'],lang('Notifications for application of network lines'),$temp,1,0);
                    }
                }else{  //网络开通申请
                    $vars['noticeType'] = 22;
                    $vars['title'] = lang("Hello, your network line has passed.");
                    $temp = $this->fetch('message/temp',$vars);
                    Messages::Notice($vars['user_id'],lang('Notifications for application of network lines'),$temp,1,0);
                    /*推送给上级*/
                    $sup_id = Users::getSupId($vars['user_id']);
                    $vars['noticeType'] = 2;
                    $vars['surname'] = Users::getEmail($vars['user_id']);
                    $vars['title'] = lang("Hello,%s's application for network adjustment has passed.",[$vars['surname']]);
                    $temp = $this->fetch('message/temp',$vars);
                    Messages::Notice($sup_id,lang('Notifications for application of network lines'),$temp,1,1);
                    if(isset($vars['engineeId']) && $vars['engineeId'] > 0){
                        $vars['title'] = lang("Hello, your configured network line configuration has been passed by the administrator.");
                        $temp = $this->fetch('message/temp',$vars);
                        Messages::Notice($vars['engineeId'],lang('Notifications for application of network lines'),$temp,1,0);
                    }
                }
                break;
            case 4:  //推送消息给客户和他的上级
                # code...
                if($vars['pre_status'] == 8){ //网络调整
                    $vars['title'] = lang("Hello! Your application for the broadband adjustment of the line is returned:");
                    $vars['noticeType'] = 4;
                    $temp = $this->fetch('message/temp',$vars);
                    Messages::Notice((int)$vars['user_id'],lang('Network line adjustment return notice'),$temp,4,0);  //客户
                    /*推送给上级*/
                    $sup_id = Users::getSupId($vars['user_id']);
                    $vars['surname'] = Users::getEmail($vars['user_id']);
                    $vars['noticeType'] = 44;
                    $vars['title'] = lang("Your customer line broadband adjustment application is returned:");
                    $temp = $this->fetch('message/temp',$vars);
                    Messages::Notice((int)$sup_id,lang('Network line adjustment return notice'),$temp,4,1);  //客户
                }else{
                    $vars['title'] = lang("Hello! Your application for the line is returned:");
                    $vars['noticeType'] = 4;
                    $temp = $this->fetch('message/temp',$vars);
                    Messages::Notice((int)$vars['user_id'],lang('Notifications of application return on network line opening'),$temp,1,0);  //客户
                    /*推送给上级*/
                    $sup_id = Users::getSupId($vars['user_id']);
                    $vars['surname'] = Users::getEmail($vars['user_id']);
                    $vars['noticeType'] = 44;
                    $vars['title'] = lang("Your customer line application is returned:");
                    $temp = $this->fetch('message/temp',$vars);
                    Messages::Notice((int)$sup_id,lang('Notifications of application return on network line opening'),$temp,1,1);  //客户
                }
                break; 
            case 8:  //推送消息给admin/他的上级
                # code...
                $vars['surname'] = Users::getEmail($vars['user_id']);
                $vars['title'] = lang("Hello! Application notification of customer line broadband adjustment");
                $sup_id = Users::getSupId($vars['user_id']);
                $vars['noticeType'] = 2;
                $temp = $this->fetch('message/temp',$vars);
                Messages::Notice(($sup_id > 0 ? $sup_id : 1),lang('Notifications for application of network line adjustment'),$temp,2,2);
                break;
            case 10: //推送消息给admin/他的上级
                # code...
                $vars['surname'] = Users::getEmail($vars['user_id']);
                $vars['title'] = lang("Hello! Customer line deletion application notification:");
                $sup_id = Users::getSupId($vars['user_id']);
                $vars['noticeType'] = 2;
                $temp = $this->fetch('message/temp',$vars);
                Messages::Notice(($sup_id > 0 ? $sup_id : 1),lang('Network line deletion application notice'),$temp,2,2);
                break;
            case 12: //推送消息给客户/他的上级
                # code...
                $vars['title'] = lang("Hello! Your network line deletion application has been passed:");
                $vars['noticeType'] = 22;
                $temp = $this->fetch('message/temp',$vars);
                Messages::Notice((int)$vars['user_id'],lang('Network line deletion application'),$temp,2,0);  //客户
                /*推送给上级*/
                $sup_id = Users::getSupId($vars['user_id']);
                $vars['surname'] = Users::getEmail($vars['user_id']);
                $vars['noticeType'] = 2;
                $vars['title'] = lang("Your customer network line deletion application has passed:");
                $temp = $this->fetch('message/temp',$vars);
                Messages::Notice((int)$sup_id,lang('Network line deletion application'),$temp,2,0);  //客户
                break;
            case 14: //推送消息给客户/他的上级
                # code...
                $vars['title'] = lang("Hello! Your network line deletion application is returned:");
                $vars['noticeType'] = 4;
                $temp = $this->fetch('message/temp',$vars);
                Messages::Notice((int)$vars['user_id'],lang('Line deletion application notice'),$temp,2,1);  //客户
                /*t推送给上级*/
                $sup_id = Users::getSupId($vars['user_id']);
                $vars['surname'] = Users::getEmail($vars['user_id']);
                $vars['noticeType'] = 44;
                $vars['title'] = lang("Your customer network line deletion application is returned:");
                $temp = $this->fetch('message/temp',$vars);
                Messages::Notice((int)$sup_id,lang('Line deletion application notice'),$temp,2,0);  //客户
                break;
            case 18://多个
                # code...
                $vars['noticeType'] = 18;
                $vars['title'] = lang("Application notification of customer network line opening:");
                $this->assign('networks',$vars['networks']);
                unset($vars['networks']);
                $temp = $this->fetch('message/temp',$vars);
                Messages::Notice((int)$sup_id,lang('Notifications for application of network lines'),$temp,1,1);
                break;
            case 20://线路续期提醒
                # code...
                $vars['noticeType'] = 20;
                $vars['title'] = lang("Reminder Notice of Line Extension").":";
                $vars['cs_email'] = $this->user['email'];
                $temp = $this->fetch('message/temp',$vars);
                Messages::Notice((int)$vars['user_id'],lang('Reminder Notice of Line Extension'),$temp,5,0);
                break;
            default:
                # code...
                break;
        }
    }
    
}
